package edu.ynu.hhy.architectureclass.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import edu.ynu.hhy.architectureclass.common.entity.LogicEntity;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Getter
@Setter
@Entity
@Where(clause = "is_deleted = 0")
public class Device extends LogicEntity implements Serializable {

    @Column(nullable = false)
    private String name;

    @Column(name = "serial_number", nullable = false, unique = true) // 确保列名与数据库一致
    private String serialNumber; // 设备的序列号

    @Column(nullable = false)
    private String category;//"sensor"或"camera"

    @Column(nullable = true)
    private String boxNo; // 仅传感器设备需要

    @Column
    private String unit;

    @Column
    private String monitorAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "greenhouse_id")
    @JsonIgnoreProperties("devices")
    private Greenhouse greenhouse;

    @OneToMany(mappedBy = "device", fetch = FetchType.LAZY)
    @JsonIgnoreProperties("device")
    private List<SensorData> sensorDataList; // 关联的传感器数据

    @OneToMany(mappedBy = "device", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnoreProperties("device")
    private Set<CameraData> cameraData; // 关联的摄像头数据

    @Temporal(TemporalType.TIMESTAMP)
    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date createTime; // 创建时间

    @Override
    public String toString() {
        return "Device{" +
                "id=" + getId() +
                ", name='" + name + '\'' +
                ", serialNumber='" + serialNumber + '\'' +
                ", category='" + category + '\'' +
                ", boxNo='" + boxNo + '\'' +
                ", unit='" + unit + '\'' +
                ", monitorAddress='" + monitorAddress + '\'' +
                ", createTime=" + getCreateTime() +
                ", updateTime=" + getUpdateTime() +
                ", isDeleted=" + getIsDeleted() +
                '}';
    }
}
